#################################
# Task: Plot Time Series of Mean Vote Shares for authoritarian populist parties across WVS Dems
# Date: February 17th 2021
#################################


# Packages

library(readxl) # 1.0.0
library(tidyr)
library(dplyr)
library(ggplot2)
library(countrycode)
library(ggrepel)



# Read in xlsx file
#------------------

vote.share <- as.data.frame(read_excel("auth_rwp_shares_final.xlsx", sheet = 1, range = "A3:I19", col_names = FALSE, na = 'NA'))
names(vote.share) <- c("country","1980- 1984", "1985- 1989","1990- 1994", "1995- 1999", "2000- 2004", "2005- 2009", "2010- 2014","2015- 2018")


# create the vectors period and share

vote.share <- vote.share %>%
  gather(period, share, -country) %>%
  mutate(share = as.numeric(share))

# create new object share per period, remove NAs

share_per_period <- vote.share %>%
  group_by(period) %>%
  summarise(mean_share = mean(share, na.rm = TRUE),
            sd_share = sd(share, na.rm = TRUE)) %>%
  ungroup() %>%
  mutate(mean_share = ifelse(is.na(mean_share), 0, mean_share),
         sd_share = ifelse(is.na(sd_share), 0, sd_share))


# convert country to isocountry

vote.share$country <- countrycode(vote.share$country, origin = "country.name", destination = "iso3c")




# plot figure 6 in paper
#--------------------------------------------
# add country names in plot

ggplot(data = vote.share, aes(x = period, y = share, group = 1))+
  geom_point() +
  geom_text_repel(aes(label = country))+
  geom_hline(yintercept = 0, linetype = "dashed")+
  stat_summary(geom = "pointrange", fun.data = "mean_cl_boot", colour = "red", label = vote.share$country, na.rm = T, size = 1)+
  stat_summary(geom = "line", fun.data = "mean_cl_boot", colour = "red", na.rm = T, group = 1)+
  labs(x = "Period", y= "Share")+
  theme_bw()+
  ggtitle("Mean Share of Votes for Authoritarian Populist Parties across WVS Democracies")

ggsave(file = "Mean Share of Votes auth pop parties_WVS DEMS with names_Sep20.png", height = 20, width = 30, units = "cm")




